//
//  MusicTableViewController.m
//  XMLY
//
//  Created by lanou on 15/11/4.
//  Copyright © 2015年 曾杰. All rights reserved.
//

#import "MusicTableViewController.h"
#import "MusicViewCell.h"
#import "PKRequestManager.h"
#import "MusicDetailViewController.h"
#import "MJRefresh.h"
@interface MusicTableViewController ()
@property (nonatomic, strong)NSMutableArray * dataArr;
@property (nonatomic,strong)MusicDetailViewController * detailVC;
@end

@implementation MusicTableViewController

-(NSMutableArray *)dataArr
{
    if (_dataArr==nil) {
        self.dataArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArr;
}

-(void)requestMusicData
{
    NSString * pageId =[NSString stringWithFormat:@"pageId=%ld",self.dataArr.count/20+1];
    NSRange range = [self.urlStr rangeOfString:@"pageId=1"];
    NSString * newUrl = [self.urlStr stringByReplacingCharactersInRange:range withString:pageId];
    [PKRequestManager requestWithType:GET urlString:newUrl parDic:nil finish:^(NSData *data) {
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray * array = [dic objectForKey:@"list"];
        for (NSDictionary * dict in array) {
            MusicModel * model = [[MusicModel alloc]init];
            [model setValuesForKeysWithDictionary:dict];
            [self.dataArr addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    } error:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestMusicData];
    [self.tableView registerClass:[MusicViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.rowHeight = self.view.bounds.size.height/6+20;
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestMusicData];
        
    }];
    
   
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
              [self requestMusicData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   MusicViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    MusicModel * model = self.dataArr[indexPath.row];
    [cell configMusicCellWithModel:model];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    _detailVC = [[MusicDetailViewController alloc]init];
    MusicModel * model = self.dataArr[indexPath.row];
    _detailVC.albumId = model.albumId;
    [self presentViewController:_detailVC animated:YES completion:nil];
    
    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
